package com.hugoo.core;

import com.hugoo.core.util.Prop;
import com.hugoo.core.util.TL;
import com.hugoo.form.buzz.CacheProc;
import com.hugoo.form.xo.XForm;
import com.hugoo.form.xo.XForms;
import java.io.File;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.digester.Digester;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Hugoo.Chan
 */
public class Conf {
    ////////////////////////////////////////////////////////////

    protected static final Log log = LogFactory.getLog(Conf.class);
    public static final String _jndi_key = "database.jndi";
    public static final String _rpt_file_key = "rpt.conf.path";
    public static final String _imp_file_key = "imp.conf.path";
    public static final String _imp_file_space = "imp.path";
    public static final String RPT_CONFIG = "rpt-config";
    
    public static class RPT {
        
        public static final String _rpt_ver = "rpt.ver";//版本
        public static final String _rpt_ver_para = "rpt.ver.para";//版本参数
        public static final String _rpt_title = "rpt.title";//报表标题，用于附件导出时的文件头    
        public static final String _rpt_exp_need_title = "rpt.exp.need.title";//报表标题，用于附件导出时的文件头    
        public static final String _rpt_exp_need_end = "rpt.exp.need.end";//报表标题，用于附件导出时的文件头    
        public static final String _rpt_location = "rpt.location";//报表所处位置，用于页面导航使用，譔项为必填项
        public static final String _rpt_sql_templete = "rpt.sql";//报表数据源模板
        public static final String _rpt_sql_para_column = "rpt.sql.para";//报表数据源参数列表
        public static final String _rpt_sql_filter_tpl = "rpt.filter.tpl";//报表数据过滤模板    
        public static final String _rpt_sql_filter_fields = "rpt.filter";//报表数据过滤项列表
        public static final String _rpt_htm_sort_cols = "rpt.htm.sort.col";
        public static final String _rpt_htm_frmb_cols = "rpt.htm.fmt.rmb";//格式化为钱
        public static final String _rpt_htm_fpct_cols = "rpt.htm.fmt.pct";//格式化为钱
        public static final String _rpt_htm_fnum_cols = "rpt.htm.fmt.num";//格式化为钱
        public static final String _rpt_xls_fnum_cols = "rpt.xls.fmt.num";//xls中格式化为数值
        public static final String _rpt_xls_tpl = "rpt.xls.tpl";//xls模板文件
        //报表数据项列表信息，本列表采用[数据项列名]:[数据项标题]:[样式名1|样式名2][,...]
        public static final String _rpt_columns = "rpt.column";
        public static final String _rpt_xls_columns = "rpt.xls.column";//
        public static final String _rpt_txt_columns = "rpt.txt.column";
        public static final String _rpt_htm_columns = "rpt.htm.column";
        //数据源
        public static final String _rpt_dbl = "rpt.dbl";
        //
        public static final String _rpt_htm_tpl = "rpt.tpl";//报表页模板
        public static final String _rpt_htm_pagesize = "rpt.htm.pagesize"; //报表页记录缺省值
        //自定义
        public static final String _rpt_htm_custom_thead = "rpt.htm.custom.thead";//表头，若本项存在，将以本项为准，生成表头
        public static final String _rpt_htm_custom_tbody_tpl = "rpt.htm.custom.tbody.tpl"; //自定义表身模板    
        public static final String _rpt_htm_custom_colnum = "rpt.htm.custom.colnum"; //自定义表身模板    
        public static final String _rpt_htm_ref_columns = "rpt.htm.ref.col";//引用列，用于后台传递参数之前
    }
    ///

    public static class IMP {
        
        public static final String _imp_title = "imp.title";
        public static final String _imp_table = "imp.table";
        public static final String _imp_table_column = "imp.column";
        public static final String _imp_table_al_column = "imp.al.column";
        public static final String _imp_pre_op = "imp.pre";
        public static final String _imp_lst_op = "imp.lst";
        public static final String _imp_type = "imp.type";
        public static final String _imp_methods = "imp.method";
        public static final String _imp_cust_tpl = "imp.cust.tpl";
        public static final String _imp_cpl_url = "imp.cpl.url";
    }
    
    public static class FTP {

        //proxy setting
        public static final String _proxy_server = "ftp.proxy.host";
        public static final String _proxy_port = "ftp.proxy.port";
        public static final String _proxy_user = "ftp.proxy.user";
        public static final String _proxy_passwd = "ftp.proxy.passwd";
        //ftp info
        public static final String _ftp_host = "ftp.host";
        public static final String _ftp_server = "ftp.server";
        public static final String _ftp_port = "ftp.port";
        public static final String _ftp_user = "ftp.user";
        public static final String _ftp_passwd = "ftp.passwd";
        //SSL protocol
        public static final String _ftp_protocol = "ftp.protocol";
        public static final String _ftp_trustmgr = "ftp.trustmgr";
        public static final String _ftp_keepAliveTimeout = "ftp.keepAliveTimeout";
        public static final String _ftp_controlKeepAliveReplyTimeout = "ftp.controlKeepAliveReplyTimeout";
        //
        public static final String _ftp_workspace = "ftp.workspace";
        public static final String _ftp_filename_pattern = "ftp.filename.pattern";
    }
    
    public static class SCH {//调度

        public static final String _sch_oper_type = "sch.type";//UPLOAD|DOWNLOAD
        public static final String _sch_cycle = "sch.cycle";//操作周期
        public static final String _schs = "sch.lists";//
        public static final String _schs_exec_proc = "exec.proc";//
        public static final String _schs_exec_dbl = "exec.dbl";//
    }
    
    public static class FORM {
        
        public static final String _cb_para = "cb.para";
        public static final String _cb_cfg = "cb.cfg";
        public static final String _cb_type = "cb.type";
        public static final String _cb_db_sql = "cb.db.sql";
        public static final String _cb_dbl = "cb.dbl";
    }
    
    public static class TREE {
        
        public static final String _xx_id = "stree.id";
        public static final String _xx_desc = "stree.desc";
        public static final String _xx_para = "stree.para";
        public static final String _xx_table = "stree.table";
        public static final String _xx_sw = "stree.sw";
        public static final String _xx_prior = "stree.prior";
        public static final String _xx_key = "stree.key";
        public static final String _xx_dbl = "stree.dbl";//数据库连接实例
    }
    private final static Map<String, String> dvalm = new ConcurrentHashMap<String, String>(4096);
    private final static Map<String, XForm> xformMap = new ConcurrentHashMap<String, XForm>(4096);

//    static {
//       reload("/prop/conf.properties");
//    }
    public static XForm getXForm(String key) {
        return xformMap.get(key);
    }
    
    public static boolean reload(String path) {
        try {
            //缓存数据-清除
            CacheProc.clearCacheData();
            log.debug("清除缓存数据完成!");
            //程序数据-清除
            dvalm.clear();
            log.debug("清除配置文件程序数据完成!");
            //
            xformMap.clear();
            log.debug("清除表单配置数据完成!");
            //----
            Prop.load(path).to(dvalm);
            String p1 = dvalm.get(Conf._imp_file_key), p2 = dvalm.get(Conf._rpt_file_key);
            Prop.load(p1, p2).to(dvalm);
            log.debug("加载配置文件程序数据完成!");
            ///
            String fmp = getBaseProp("form.path");
            if (TL.isEmpty(fmp)) {
                log.info("未配置FORM表单路径!");
            } else {
                String fp[] = fmp.split(",");
                for (String f : fp) {
                    log.debug("加载表单配置文件" + f);
                    File fx = new File(Prop.class.getResource(f).getFile());
                    if (fx.exists()) {
                        XForms fm = parseXForms(fx);
                        xformMap.putAll(fm.getXformMap());
                    }
                }
            }
            log.info("加载程序原始数据完毕@_@!!");
        } catch (Exception e) {
            log.error("加载初始化信息出错！", e);
            return false;
        }
        return true;
    }
    
    private static String get(String sid, String key) {
        return get(sid, key, false);
    }
    
    private static String get(String sid, String key, boolean findPrev) {
        if (findPrev) {
            String k = dvalm.get(sid + "." + key);
            //如果返回的结果集为空，并且是可以找到子集的情况下，直接加载子集
            if (k == null && !"default".equals(sid)) {
                return dvalm.get("default." + key);
            } else {
                return k;
            }
        } else {
            return dvalm.get(sid + "." + key);
        }
    }
    
    private static XForms parseXForms(File f) throws Exception {
        Digester digester = new Digester();
        digester.setValidating(false);
        digester.addObjectCreate("xForms", "com.hugoo.form.xo.XForms");
        String[][] keys = {
            {"xForms/xForm", "addXForm", "com.hugoo.form.xo.XForm"},
            {"xForms/xForm/xHandle", "addXHandle", "com.hugoo.form.xo.XHandle"},
            {"xForms/xForm/xHandle/xSql", "addXSql", "com.hugoo.form.xo.XSql"},
            {"xForms/xForm/xHandle/xSql/xFlt", "addXFlt", "com.hugoo.form.xo.XFlt"},
            {"xForms/xForm/xHandle/xSql/xFlt/xFltPara", "addXFltPara", "com.hugoo.form.xo.XFltPara"}
        };
        setDigester(digester, keys);
        return (XForms) digester.parse(f);
    }
    
    private static Digester setDigester(Digester digester, String[][] keys) {
        for (String[] ks : keys) {
            digester.addObjectCreate(ks[0], ks[2]);
            digester.addSetProperties(ks[0]);
            digester.addSetNext(ks[0], ks[1], ks[2]);
        }
        return digester;
    }
    
    public static String getBaseProp(String key) {
        return dvalm.get(key);
    }
    
    public static String getBaseProp(String sid, String key) {
        return Conf.get(sid, key, false);
    }
    
    public static String getAutqProp(String inst, String key) {
        return Conf.get(inst, key, true);
    }
    
    public static String getAutqProp2(String inst, String key) {
        return Conf.get(inst, key, false);
    }
    
    public static String getAutqProp2(String inst, String version, String key) {
        if (TL.isEmpty(version)) {
            return getAutqProp2(inst, key);
        }
        String vl = Conf.get(inst + "." + version, key, false);
        if (TL.isEmpty(vl)) {
            log.debug("未找到高版本配置" + inst + "." + version + ".转为默认版本." + inst);
            vl = Conf.get(inst, key, false);
        }
        return vl;
    }
    
    public static String getImpProp(String inst, String key) {
        return Conf.get(inst, key, false);
    }
}
